package org.lq.ht.view.goods;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.table.DefaultTableModel;

import org.lq.ht.entity.Goods;
import org.lq.ht.service.GoodsService;
import org.lq.ht.service.impl.GoodsServiceImpl;
import org.lq.ht.util.CastUtils;
import org.lq.ht.util.ShowMessage;

import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;

import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class goodsMain extends JFrame {

	private JPanel contentPane;
	private static JTable table;//表格声明成静态的
	private JTextField txt_query;
	private GoodsService gs = new GoodsServiceImpl();//声明商品业务层
	
	/**
	 * 获取选中商品的ID
	 */
	private int goodsId;
	private JCheckBox ck_greater;
	private JButton btn_search;
	public int getGoodsId() {
		return goodsId;
	}

	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					goodsMain frame = new goodsMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public goodsMain() {
		setTitle("商品展示");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 775, 510);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("条形码/名称：");
		panel.add(lblNewLabel);
		
		txt_query = new JTextField();
		panel.add(txt_query);
		txt_query.setColumns(10);
		
		btn_search = new JButton("查询");
		btn_search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//模糊查询
				//正则表达式
				String value =txt_query.getText();
				List<Goods> list = gs.query(value);
				initTable(list);//将拿到的集合进行表格初始化
			}
		});
		panel.add(btn_search);
		
		JButton btn_findAll = new JButton("全部商品");
		btn_findAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//查询全部商品
				initTable(gs.findAll());
				txt_query.setText("");
			}
		});
		panel.add(btn_findAll);
		
		ck_greater = new JCheckBox("只显示库存大于0");
		panel.add(ck_greater);
		
		JButton btn_add = new JButton("添加商品");
		btn_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				goodsAdd add = new goodsAdd();
				add.setBounds(goodsMain.this.getX(), goodsMain.this.getY(), add.getWidth(), add.getHeight());
				add.setVisible(true);
				//监听窗口是否关闭
				add.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						//当窗口被关闭的时候进行初始化
						initTable(gs.findAll());
					}
				});
			}
		});
		panel.add(btn_add);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		JPopupMenu popupMenu = new JPopupMenu();
		JMenuItem mntmNewMenuItem = new JMenuItem("选择");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//选择
				int index = table.getSelectedRow();
				if(index>=0) {
					goodsId =CastUtils.castInt(table.getValueAt(index, 0));
					dispose();
				}
			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("删除");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if(index>=0) {
					int id =CastUtils.castInt(table.getValueAt(index, 0));
					ShowMessage.frameMessage(goodsMain.this, gs.deleteGoodsById(id), "删除");
					initTable(gs.findAll());
				} 
			}
		});
		popupMenu.add(mntmNewMenuItem_1);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"id", "\u7F16\u53F7", "\u540D\u79F0", "\u552E\u4EF7", "\u5E93\u5B58", "\u79EF\u5206"
			}
		));
		table.getColumnModel().getColumn(0).setMinWidth(1);
		table.getColumnModel().getColumn(0).setMaxWidth(1);
		scrollPane.setViewportView(table);
		List<Goods> list = gs.findAll();
		initTable(list);
		addPopup(table, popupMenu);
	}
	/**
	 * 初始化表格
	 * @param list
	 */
	public void initTable(List<Goods> list) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount();
		for (int i = 0; i < count; i++) {
			dtm.removeRow(0);
		}
		boolean selected = this.ck_greater.isSelected();//选中 只显示库存大于零
		
		for (Goods g : list) {
			if(selected==true && g.getInventory()<=0) {//按钮被选中并且你的商品库存小于等于零  则跳出循环
				 continue;
			}
			dtm.addRow(new Object[] {
					g.getNo(),
					g.getNumber(),
					g.getName(),
					g.getSell_price(),
					g.getInventory(),
					g.getIntegral()
					
			});
		}
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				int index = table.rowAtPoint(e.getPoint());
				table.setRowSelectionInterval(index, index);
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
